package com.ruoyi.finance.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.finance.domain.BuyPayReqRefCgrk;
import com.ruoyi.finance.mapper.BuyPayReqRefCgrkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.finance.mapper.BuyPayReqMapper;
import com.ruoyi.finance.domain.BuyPayReq;
import com.ruoyi.finance.service.IBuyPayReqService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购付款申请Service业务层处理
 *
 * @author lvql
 * @date 2024-11-07
 */
@Service
public class BuyPayReqServiceImpl implements IBuyPayReqService {
    @Autowired
    private BuyPayReqMapper buyPayReqMapper;

    @Autowired
    private BuyPayReqRefCgrkMapper buyPayReqRefCgrkMapper;

    /**
     * 查询采购付款申请
     *
     * @param id 采购付款申请主键
     * @return 采购付款申请
     */
    @Override
    public BuyPayReq selectBuyPayReqById(Integer id) {
        BuyPayReq buyPayReq = buyPayReqMapper.selectBuyPayReqById(id);
        String code = buyPayReq.getCode();
        BuyPayReqRefCgrk buyPayReqRefCgrk = new BuyPayReqRefCgrk();
        buyPayReqRefCgrk.setPayReqCode(code);
        List<BuyPayReqRefCgrk> buyPayReqRefCgrks = buyPayReqRefCgrkMapper.selectBuyPayReqRefCgrkList(buyPayReqRefCgrk);
        buyPayReq.setList(buyPayReqRefCgrks);
        return buyPayReq;
    }

    /**
     * 查询采购付款申请列表
     *
     * @param buyPayReq 采购付款申请
     * @return 采购付款申请
     */
    @Override
    public List<BuyPayReq> selectBuyPayReqList(BuyPayReq buyPayReq) {
        return buyPayReqMapper.selectBuyPayReqList(buyPayReq);
    }

    /**
     * 新增采购付款申请
     *
     * @param buyPayReq 采购付款申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBuyPayReq(BuyPayReq buyPayReq) {
        buyPayReq.setCreateTime(DateUtils.getNowDate());
        List<BuyPayReqRefCgrk> list = buyPayReq.getList();
        BigDecimal bigDecimal = new BigDecimal(0);
        for (BuyPayReqRefCgrk buyPayReqRefCgrk : list) {
            buyPayReqRefCgrk.setPayReqCode(buyPayReq.getCode());
            bigDecimal = bigDecimal.add(buyPayReqRefCgrk.getRequestAmount());
            buyPayReqRefCgrkMapper.insertBuyPayReqRefCgrk(buyPayReqRefCgrk);
        }
        buyPayReq.setRequestAmount(bigDecimal);
        buyPayReq.setPaidAmount(BigDecimal.ZERO);
        buyPayReq.setApprovalResult(0);
        buyPayReq.setIsEffect(0);
        buyPayReq.setIsClose(0);
        buyPayReq.setIsCancel(0);
        buyPayReq.setDocMakeUserId(Math.toIntExact(SecurityUtils.getUserId()));
        buyPayReq.setCreateBy(SecurityUtils.getUsername());
        if (buyPayReq.getCusId() != null && buyPayReq.getCusId() > 0) {
//            buyPayReq.setSourceCode(buyPayReq.getList().get(0).getSourceCode());
            buyPayReq.setCodeType(22);
        }else {
            buyPayReq.setCodeType(21);
        }
        return buyPayReqMapper.insertBuyPayReq(buyPayReq);
    }

    /**
     * 修改采购付款申请
     *
     * @param buyPayReq 采购付款申请
     * @return 结果
     */
    @Override
    public int updateBuyPayReq(BuyPayReq buyPayReq) {
        buyPayReq.setUpdateTime(DateUtils.getNowDate());
        BigDecimal bigDecimal = new BigDecimal(0);
        for (BuyPayReqRefCgrk buyPayReqRefCgrk : buyPayReq.getList()) {
            bigDecimal = bigDecimal.add(buyPayReqRefCgrk.getRequestAmount());
            buyPayReqRefCgrkMapper.updateBuyPayReqRefCgrk(buyPayReqRefCgrk);
        }
        buyPayReq.setRequestAmount(bigDecimal);
        return buyPayReqMapper.updateBuyPayReq(buyPayReq);
    }

    /**
     * 批量删除采购付款申请
     *
     * @param ids 需要删除的采购付款申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBuyPayReqByIds(Integer[] ids) {
        List<BuyPayReq> buyPayReqs = buyPayReqMapper.selectBuyPayReqByIds(ids);
        List<String> codeList = new ArrayList<>();
        for (BuyPayReq buyPayReq : buyPayReqs) {
            codeList.add(buyPayReq.getCode());
        }
        buyPayReqRefCgrkMapper.deleteBuyPayReqRefCgrkByPayReqCodes(codeList);
        return buyPayReqMapper.deleteBuyPayReqByIds(ids);
    }

    /**
     * 删除采购付款申请信息
     *
     * @param id 采购付款申请主键
     * @return 结果
     */
    @Override
    public int deleteBuyPayReqById(Integer id) {
        return buyPayReqMapper.deleteBuyPayReqById(id);
    }

    @Override
    public int approvalBuyPayReq(BuyPayReq buyPayReq) {
        buyPayReq.setApprovalUserId(Math.toIntExact(SecurityUtils.getUserId()));
        return buyPayReqMapper.updateBuyPayReq(buyPayReq);
    }
}
